home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12362 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c++,comp.lang.eiffel,comp.lang.c,comp.object,comp.software-eng
  4. Subject: Re: Beware of "C" Hackers -- A rebuttal to Bertrand Meyer
  5. Date: 19 Mar 1996 09:47:27 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4imrvfINNqjo@keats.ugrad.cs.ubc.ca>
  8. References: <1995Jul3.034108.4193@rcmcon.com> <3taaha$p8j@ixnews3.ix.netcom.com> <4il72s$ena@news4.digex.net>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4il72s$ena@news4.digex.net>, Ell <ell@access4.digex.net> wrote:
  12. >Robert C. Martin (rmartin@oma.com) wrote:
  13. >: What is "low-level C software"?  What are the "special techniques and
  14. >: tricks"? 
  15. >
  16. >Imo, bit twiddling, liberal use of unions, and fancy pointer arithmetic
  17. >when not really necessary. 
  18.  
  19. Bit twiddling is OK when it doesn't involve implementation-defined behavior. I
  20. don't see why not. On the other hand, if you assume a particular storage layout
  21. for a bitfield, say, then you are hacking!
  22.  
  23. IMHO, unions are not used enough by C programmers. A union is the true way to
  24. implement a generic data type, such as a polymorphic dictionary data structure,
  25. say, which can store a pointer to an arbitrary structure, as well as any
  26. integral or floating-point type. How else can you do it efficiently in C other
  27. than:
  28.  
  29. typedef union {
  30.     double realval;
  31.     long longval;
  32.     unsigned long ulongval;
  33.     void *ptrval;
  34. } element;
  35.  
  36. You can use a structure, but then it takes up extra space.
  37.  
  38. Some programmers will rather just use void * and then store integers by casting
  39. them to this void *.  That is hacking, because it assumes that a void * has
  40. enough bits to hold an integer. Whether or not this is true is
  41. implementation-defined.
  42.  
  43. Or do you mean by ``liberal use of unions'' a violation whereby one type is
  44. written in the union and read through another? This calls for undefined
  45. behavior.
  46.  
  47. -- 
  48.  
  49.